feat: GitHub Enterprise Server (GHES) support#326
Open
raghavpillai wants to merge 5 commits intobetter-auth:mainfrom
Open
feat: GitHub Enterprise Server (GHES) support#326raghavpillai wants to merge 5 commits intobetter-auth:mainfrom
raghavpillai wants to merge 5 commits intobetter-auth:mainfrom
Conversation
- Introduce github-config to centralize GitHub/GHES endpoint configuration - Replace hardcoded github.com URLs with configurable GITHUB_* constants - Add GHES-aware image component (ghes-image) and avatar proxy endpoint (/api/github-avatar) - Proxy/rewrote raw, API, GraphQL, and web endpoints to use config - Wire GHES-aware avatar handling via github-avatar helper and githubAvatarUrl - Use GITHUB_* URLs across many components, pages, and API routes - Add generic OAuth provider integration and update auth plugin usage - Add GHES session login flow to fetch avatar images requiring cookie auth - Swap next/image imports to GHES-aware component where needed - Small misc: caching, error logging, and auth client plugin adjustments
- Change mapProfileToUser return to an explicit Record<string, unknown> - Wrap returned object in parentheses and add type assertion to satisfy typings - No functional change, resolves TypeScript type inference issues
- Change API error responses to JSON for github-avatar route - Retry GHES avatar fetch once when session may have expired and refresh cookies - Remove redundant HTML-response session-clear and use retry logic instead - Return JSON on fetch failure for consistent error format - Fix redis caching call to store raw user data instead of stringified JSON - Add error throw when GitHub /user response is not ok before parsing JSON
|
@raghavpillai is attempting to deploy a commit to the better-auth Team on Vercel. A member of the Team first needs to authorize it. |
- Remove deprecated ghes-image component - Add new github-image component that proxies GHES avatar URLs when needed - Update numerous imports across app to use github-image instead of ghes-image - Preserve proxy behavior and Next/Image passthrough for non-GHES hosts
- Replace manual fetch calls with Octokit client for GitHub API - Use octokit.users.getAuthenticated and listEmailsForAuthenticatedUser - Determine primary verified email and fallback to available emails - Preserve returned user shape (id, name, email, image, login)
Member
|
Hey this is awesome work! I'm keeping your PR open for now until we decide where Better Hub will go in terms of GHES support. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for self-hosted GitHub Enterprise Server instances. When configured via environment variables, all GitHub API calls, OAuth flows, avatar loading, and UI links point at the GHES host instead of github.com. When no GHES env vars are set, behavior is identical to today.
Closes #325
Problem
Every GitHub URL in the codebase is hardcoded to
github.com/api.github.com. This makes it impossible to use Better Hub with a GHES instance. GHES also has some quirks —/api/v3and/api/graphqllive on the same host, and private-mode instances require web session cookies (not API tokens) to fetch avatar images.Changes
Centralized GitHub URL config (
src/lib/github-config.ts)GITHUB_WEB_URL,GITHUB_API_URL,GITHUB_GRAPHQL_URLfrom env varsIS_GHES,GITHUB_HOSTNAME, and URL constantsGeneric OAuth (
src/lib/auth.ts,src/lib/auth-client.ts,src/components/login-button.tsx)socialProviders.githubwithgenericOAuthplugin so authorization and token endpoints can be configured per-instancegetUserInfofetches from the configurable API URL with response status validationsignIn.oauth2({ providerId: "github" })GHES avatar proxy (
src/app/api/github-avatar/route.ts,src/components/shared/ghes-image.tsx)GhesImagecomponent wrapsnext/imageto transparently route GHES avatar URLs through the proxygithubAvatarUrl()helper exported for raw<img>tagsUI updates (~60 component files)
https://github.comreferences replaced with configurableGITHUB_WEB_URL/NEXT_PUBLIC_GITHUB_WEB_URLnext.config.tsupdated to add GHES hostname toimages.remotePatternsOther
src/lib/github.tsnow use configurablebaseUrland GraphQL endpoint.env.exampleupdated with GHES configuration variablesgetOctokitUserfixed — removed double JSON serialization that was causing cache missesConfiguration
Validation
bun run buildpasses cleanlyNotes
genericOAuthplugin is a drop-in replacement forsocialProviders.github— the OAuth flow is identical, just with configurable endpointsIS_GHESis true, so there's zero overhead for github.com usersgithub.comreferences in the diff are intentional defaults/fallbacks ingithub-config.tsandnext.config.ts